import moment from 'moment'

// 格式化时间
export const dateFormat = (value, format = 'YYYY-MM-DD') => {
  if (!value) return ''
  return moment(value).format(format)
}

// 货币格式化
export const currency = (s) => {
  if (/[^0-9]\./.test(s)) return 'invalid value'
  s = s.toString()
  s = s.replace(/^(\d*)$/, '$1.')
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1')
  s = s.replace('.', ',')
  var re = /(\d)(\d{3},)/
  while (re.test(s)) {
    s = s.replace(re, '$1,$2')
  }
  s = s.replace(/,(\d\d)$/, '.$1')
  return s.replace(/^\./, '0.').toString()
}

// 百分号格式化
export const percent = (num, fixPoint) => {
  if (!fixPoint) {
    fixPoint = 2
  }
  if (!num) return '0.00%'
  num = (num * 100).toFixed(fixPoint)
  var result = num.toString() + '%'
  return result
}
